<?php

class Emms_Form_Admin_Location extends HTech_Form
{
    protected $_primary  = 'LocationId';
    
    public function __construct() 
    {
        parent::__construct(); 
        
        $this->setName('Location');
                
        $this->setAttrib('title', $this->translate("Location form"));
        $this->setDescription($this->translate("Edit location info or add a new one"));
                
        $locationId = new Zend_Form_Element_Hidden('LocationId');
                                        
        $parentLocationId = new Zend_Form_Element_Select('ParentLocationId');
        $parentLocationId->setLabel("Parent location")                
            ->setAttrib('size','4')
            ->addValidator('Callback', true, array(
                'callback' => array($this, 'checkParentCallback'),
                'messages' => $this->translate("Invalid parent location")))
            ->addMultiOptions($this->_getParentLocationOptions()); 
        if($this->_request->getPost('Category') != 'Center')
            $parentLocationId->setRequired(true)
                ->addValidator('NotEmpty', true, array('messages' => $this->translate("Required field, can't be empty")));
                
        $category = new Zend_Form_Element_Select('Category');
        $category->setLabel("Category")                
            ->setAttrib('size','3')
            ->setRequired(true)
            ->addValidator('NotEmpty', true, array('messages' => $this->translate("Required field, can't be empty")))
            ->addMultiOptions(array('Center' => $this->translate("Center"), 'Branch' => $this->translate("Branch"), 'Agency' => $this->translate("Agency")));          
        
        $code = new Zend_Form_Element_Text('Code');
        $code->setLabel("Code")
            ->setRequired(true)
            ->addValidator('NotEmpty', true, array('messages' => $this->translate("Required field, can't be empty")))
            ->addFilter('Alnum')
            ->addFilter('StringToUpper')
            ->addValidator('StringLength',false,
                array(
                    'min'      => 2,
                    'max'      => 64,
                    'messages' => $this->translate("From %min% to %max% characters only")
                ))
            ->addValidator('Db_NoRecordExists',false,
                array(
                    'table'    => 'locations', 
                    'field'    => 'Code', 
                    'exclude'  => array('field' => 'LocationId', 'value' => $this->_id),
                    'messages' => $this->translate("Non valid or duplicated entry")                     
                ));                
        
        $name = new Zend_Form_Element_Text('Name');
        $name->setLabel("Location name")
            ->setRequired(true)
            ->addValidator('NotEmpty', true, array('messages' => $this->translate("Required field, can't be empty")))
            ->addFilter('Alnum', array('allowwhitespace' => true))
            ->addValidator('StringLength',false,
                array(
                    'min'      => 2,
                    'max'      => 64,
                    'messages' => $this->translate("From %min% to %max% characters only")
                ))
            ->addValidator('Db_NoRecordExists',false,
                array(
                    'table'    => 'locations', 
                    'field'    => 'Name', 
                    'exclude'  => array('field' => 'LocationId', 'value' => $this->_id),
                    'messages' => $this->translate("Non valid or duplicated entry")                    
                ))
            ->setDescription(null);
        
        $shortName = new Zend_Form_Element_Text('ShortName');
        $shortName->setLabel("Short name")
            ->setRequired(true)
            ->addValidator('NotEmpty', true, array('messages' => $this->translate("Required field, can't be empty")))
            ->addFilter('Alpha')
            ->addFilter('StringToUpper')
            ->addValidator('StringLength',false,
                array(
                    'min'      => 2,
                    'max'      => 3,
                    'messages' => $this->translate("Enter a %max% characters value")
                ))
            ->addValidator('Db_NoRecordExists',false,
                array(
                    'table'    => 'locations', 
                    'field'    => 'ShortName', 
                    'exclude'  => array('field' => 'LocationId', 'value' => $this->_id),
                    'messages' => $this->translate("Non valid or duplicated entry")                     
                ));
            
        $address = new Zend_Form_Element_Textarea('Address');
        $address->setLabel("Address")
            ->addFilter('Alnum', array('allowwhitespace' => true))
            ->setAttrib('rows', 4); 
        
        $latitude = new Zend_Form_Element_Text('Latitude');
        $latitude->setLabel("Latitude")
            ->setRequired(true)
            ->addValidator('NotEmpty', true, array('messages' => $this->translate("Required field, can't be empty")))
            ->addValidator('Regex', true, array(
                'pattern' => '(^(-)?\d{1,3}+(\.\d{1,7})?$)',
                'messages' => $this->translate("Only valid latitude coordinates allowed")))
            ->addValidator('Between', true, array(
                'min' => $this->_conf->geo->latitude->min,
                'max' => $this->_conf->geo->latitude->max,
                'messages' => $this->translate("Out of range value")));
                                
        $longitude = new Zend_Form_Element_Text('Longitude');
        $longitude->setLabel("Longitude")
            ->setRequired(true)
            ->addValidator('NotEmpty', true, array('messages' => $this->translate("Required field, can't be empty")))
            ->addValidator('Regex', true, array(
                'pattern' => '(^(-)?\d{1,3}+(\.\d{1,7})?$)',
                'messages' => $this->translate("Only valid longitude coordinates allowed")))
            ->addValidator('Between', true, array(
                'min' => $this->_conf->geo->longitude->min,
                'max' => $this->_conf->geo->longitude->max,
                'messages' => $this->translate("Out of range value")));
        
        $tel1 = new Zend_Form_Element_Text('Tel1');
        $tel1->setLabel("Tel1")
            ->addValidator('Regex',false, 
                array(
                    'pattern' => $this->_conf->regex->tel->pattern,
                    'messages' => sprintf(
                        $this->translate("Valid format: %s"),$this->_conf->regex->tel->message)
                ));  
        
        $tel2 = new Zend_Form_Element_Text('Tel2');
        $tel2->setLabel("Tel2")
            ->addValidator('Regex',false, 
                array(
                    'pattern' => $this->_conf->regex->tel->pattern,
                    'messages' => sprintf(
                        $this->translate("Valid format: %s"),$this->_conf->regex->tel->message)
                ));  
                    
        $fax = new Zend_Form_Element_Text('Fax');
        $fax->setLabel("Fax")
            ->addValidator('Regex',false, 
                array(
                    'pattern' => $this->_conf->regex->tel->pattern,
                    'messages' => sprintf(
                        $this->translate("Valid format: %s"),$this->_conf->regex->tel->message)
                ));  
        
        $comments = new Zend_Form_Element_Textarea('Comments');
        $comments->setLabel("Comments")
            ->addFilter('Alnum', array('allowwhitespace' => true))
            ->setAttrib('rows', 4); 
        
        $submit = new Zend_Form_Element_Submit('Submit');
        $submit->setLabel("Submit")
            ->addDecorator(new HTech_Form_Decorator_Button(array('type'=>'submit')));
                                
        $programId = new Zend_Form_Element_Multiselect('LnkLocationsPrograms');
        $programId->setLabel("Programs")                
            ->setAttrib('size','6')
            ->setRequired(false)
            ->addMultiOptions($this->_db->fetchPairs('select ProgramId, Program from programs'));   
        
        $this->setDecorators(array(array('ViewScript', array('viewScript' => 'location/_form_add.phtml'))));
        
        $this->addElements(array($name,$shortName,$code,$category,$parentLocationId,
            $latitude,$longitude,$tel1,$tel2,$fax,$programId,$address,$comments,$submit,$locationId));
    }
    
    protected function _getParentLocationOptions()
    {   
        $this->_id = $this->_id ? $this->_id : $this->_request->getParam('id');
        return Locations::getParentLocationOptions($this->_id, $this->_request->getPost('Category'));
    }
    
    public function checkParentCallback($newParent, $args) 
    {
        if($args['Category'] != 'Center') {
            if((int) $newParent < 1) { return false; }
        }
        return true;    
    }
}